package top.hkyzf.study.leetcode;

import org.junit.Test;

/**
 * description: 
 * @author zhufeng
 * @date 2023/4/11 9:31
 * @version V1.0
 */
public class 困于环中的机器人_1041 {
    @Test
    public void testIsRobotBounded () {
        String instructions = "GGLLGG";
        boolean robotBounded = isRobotBounded(instructions);
        System.out.println(robotBounded);
    }
    public boolean isRobotBounded(String instructions) {
        // 0 - 北，1 - 东，2 - 南，3 - 西
        int[][] direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        char[] chars = instructions.toCharArray();
        // 方向，X，Y
        int[] robot = {0, 0, 0};
        for (char ins : chars) {
            switch (ins) {
                case 'G':
                    int[] dir = direction[robot[0]];
                    robot[1] = robot[1] + dir[0];
                    robot[2] = robot[2] + dir[1];
                    break;
                case 'L':
                    int prev = robot[0] - 1;
                    robot[0] = prev <= -1 ? 3 : prev;
                    break;
                case 'R':
                    int next = robot[0] + 1;
                    robot[0] = next >= 4 ? 0 : next;
                    break;
                default:
            }
        }
        // 原点
        if (robot[1] == 0 && robot[2] == 0) {
            return true;
        } else if (robot[0] == 0) {
            // 朝北
            return false;
        }
        return true;
    }

}

